package com.example.accessibilitydemo.service;

import java.util.ArrayList;
import java.util.List;

import com.example.accessibilitydemo.util.PreferenceUtil;

import android.accessibilityservice.AccessibilityService;
import android.annotation.TargetApi;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

public class BaseAccessibilityService extends AccessibilityService {

	private static final String TAG = BaseAccessibilityService.class.getSimpleName();
	/**
	 * 模拟点击事件
	 *
	 * @param nodeInfo
	 *            nodeInfo
	 */
	public void performViewClick(AccessibilityNodeInfo nodeInfo) {
		if (nodeInfo == null) {
			return;
		}
		while (nodeInfo != null) {
			if (nodeInfo.isClickable()) {
				nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
				break;
			}
			nodeInfo = nodeInfo.getParent();
		}
	}

	/**
	 * 模拟返回操作
	 */
	public void performBackClick() {
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		performGlobalAction(GLOBAL_ACTION_BACK);
	}

	/**
	 * 模拟下滑操作
	 */
	public void performScrollBackward() {
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		performGlobalAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
	}

	/**
	 * 模拟上滑操作
	 */
	public void performScrollForward() {
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		performGlobalAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
	}

	/**
	 * 模拟按HOME键操作
	 */
	public void performHomeClick() {
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		performGlobalAction(GLOBAL_ACTION_HOME);
	}

	/**
	 * 查找所有对应文本的View
	 * 
	 * @param text
	 * @return
	 */
	public List<AccessibilityNodeInfo> findAllViewByText(String text) {
		AccessibilityNodeInfo rootNode = getRootInActiveWindow();
		List<AccessibilityNodeInfo> nodeInfoList = new ArrayList<AccessibilityNodeInfo>();
		if (rootNode == null) {
			Log.w(TAG, "rootNode为空");
			return nodeInfoList;
		}
		
		nodeInfoList = rootNode.findAccessibilityNodeInfosByText(text);
		
		return nodeInfoList;
	}

	/**
	 * 查找对应文本的View
	 * 
	 * @param text
	 *            唯一值
	 * @return
	 */
	public AccessibilityNodeInfo findViewByText(String text) {
		AccessibilityNodeInfo rootNode = getRootInActiveWindow();
		if (rootNode == null) {
			Log.w(TAG, "rootNode为空");
			return null;
		}
		List<AccessibilityNodeInfo> nodeInfoList = rootNode.findAccessibilityNodeInfosByText(text);
		if (nodeInfoList != null && !nodeInfoList.isEmpty()) {
			for (AccessibilityNodeInfo nodeInfo : nodeInfoList) {
				if (nodeInfo != null) {
					return nodeInfo;
				}
			}
		}
		return null;
	}

	/**
	 * 查找对应ID的View
	 *
	 * @param id
	 *            唯一值
	 * 
	 * @return View
	 */
	@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
	public AccessibilityNodeInfo findViewByID(String id) {
		AccessibilityNodeInfo rootNode = getRootInActiveWindow();
		if (rootNode == null) {
			Log.w(TAG, "rootNode为空");
			return null;
		}
		List<AccessibilityNodeInfo> nodeInfoList = rootNode.findAccessibilityNodeInfosByViewId(id);
		if (nodeInfoList != null && !nodeInfoList.isEmpty()) {
			for (AccessibilityNodeInfo nodeInfo : nodeInfoList) {
				if (nodeInfo != null) {
					return nodeInfo;
				}
			}
		}
		return null;
	}

	/**
	 * 查找所有对应ID的View
	 *
	 * @param id
	 * 
	 * @return View
	 */
	@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
	public List<AccessibilityNodeInfo> findAllViewByID(String id) {
		AccessibilityNodeInfo rootNode = getRootInActiveWindow();
		List<AccessibilityNodeInfo> nodeInfoList = new ArrayList<AccessibilityNodeInfo>();
		if (rootNode == null) {
			Log.w(TAG, "rootNode为空");
			return nodeInfoList;
		}
		 nodeInfoList = rootNode.findAccessibilityNodeInfosByViewId(id);
		return nodeInfoList;
	}

	/**
	 * 查找满足id和text的View
	 * 
	 * @param id
	 * @param text
	 * @return
	 */
	public AccessibilityNodeInfo findView(String id, String text) {
		AccessibilityNodeInfo rootNode = getRootInActiveWindow();
		if (rootNode == null) {
			Log.w(TAG, "rootNode为空");
			return null;
		}

		List<AccessibilityNodeInfo> nodeInfoList = rootNode.findAccessibilityNodeInfosByViewId(id);
		if (nodeInfoList != null && !nodeInfoList.isEmpty()) {
			for (AccessibilityNodeInfo nodeInfo : nodeInfoList) {
				if (nodeInfo != null && text.equals(nodeInfo.getText())) {
					return nodeInfo;
				}
			}
		}
		return null;
	}

	/**
	 * 模拟点击，根据text
	 * 
	 * @param text
	 */
	public void clickViewByText(String text) {
		AccessibilityNodeInfo node = findViewByText(text);
		performViewClick(node);
	}

	/**
	 * 模拟点击，根据id
	 * 
	 * @param text
	 */
	public void clickViewByID(String id) {
		AccessibilityNodeInfo node = findViewByID(id);
		performViewClick(node);
	}

	/**
	 * 延时模拟点击，根据id
	 * 
	 * @param text
	 * @param delaytime
	 */
	public void delayClickViewByText(final String text, final long delaytime) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(delaytime);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				clickViewByText(text);
			}
		}).start();
	}

	/**
	 * 延时模拟点击，根据id
	 * 
	 * @param text
	 * @param delaytime
	 */
	public void delayClickViewByID(final String id, final long delaytime) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(delaytime);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				clickViewByID(id);
			}
		}).start();
	}

	/**
	 * 模拟输入
	 *
	 * @param nodeInfo
	 *            nodeInfo
	 * @param text
	 *            text
	 */
	@TargetApi(21)
	public void inputText(AccessibilityNodeInfo nodeInfo, String text) {
		// if (Build.VERSION.SDK_INT >= 21) {// 5.0以上
		// Bundle arguments = new Bundle();
		// arguments.putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE,
		// text);
		// nodeInfo.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT,
		// arguments);
		// } else

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
			ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
			ClipData clip = ClipData.newPlainText("label", text);
			clipboard.setPrimaryClip(clip);
			nodeInfo.performAction(AccessibilityNodeInfo.ACTION_FOCUS);
			nodeInfo.performAction(AccessibilityNodeInfo.ACTION_PASTE);
		}
	}

	public void sleep(long delaytime) {
		try {
			Thread.sleep(delaytime);
		} catch (InterruptedException mE) {
			mE.printStackTrace();
		}
	}

	@Override
	public void onAccessibilityEvent(AccessibilityEvent event) {
		PreferenceUtil.init(this);
	}

	@Override
	public void onInterrupt() {

	}

}
